//
//  DrawAllVC.m
//  Cat
//
//  Created by dllo on 15/11/30.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "DrawAllVC.h"
#import "TableViewControllerModel.h"
#import "DrawAllSecVC.h"

@interface DrawAllVC ()

@property (nonatomic, copy) NSString *verStr;
@property (nonatomic, strong) NSMutableArray *arr;

@end

@implementation DrawAllVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    
    // 下拉刷新
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(RefreshData)];
    self.tableView.header = header;
    
    // 上拉刷新
    MJRefreshAutoGifFooter *footer = [MJRefreshAutoGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(RefreshData)];
    self.tableView.footer = footer;

    [self RefreshData];
}

- (void)RefreshData {
    if ([self.tableView.footer isRefreshing]) {
        NSString *URL = [NSString stringWithFormat:@"http://api.huapipi.com/show/v2/recommend/8?deviceId=1826162&uid=1033268&ver=%@", self.verStr];
        [ToolNetWorking ToolNetWorking:URL block:^(id result) {
            
            NSMutableArray *array = [TableViewControllerModel BaseModel:result[@"rows"]];
            [self.arr addObjectsFromArray:array];
            
            // 拼接网址刷新
            HotModel *tempModel = self.arr.lastObject;
            self.verStr = tempModel.ver;
            
            [self.tableView reloadData];
            [self.tableView.footer endRefreshing];
        }];
    } else {
        [ToolNetWorking ToolNetWorking:@"http://api.huapipi.com/show/v2/recommend/8?deviceId=1826162&uid=1033268" block:^(id result) {
            self.arr = [NSMutableArray array];
            self.arr = [TableViewControllerModel BaseModel:result[@"rows"]];
            
            // 拼接网址刷新
            HotModel *tempModel = self.arr.lastObject;
            self.verStr = tempModel.ver;
            
            [self.tableView reloadData];
            [self.tableView.header endRefreshing];
        }];

    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DrawAllSecVC *secVC = [[DrawAllSecVC alloc] init];
    
    TableViewControllerModel *model = self.arr[indexPath.row];
    secVC.secid = model.secid;
    [self.navigationController pushViewController:secVC animated:YES];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden = NO;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
